#include "main.h"


int main(int argc, char *argv[])
{
    char strFileSrc[MAX_FILENAME_LEN] = "in.txt\0";
    char strFileTarget[MAX_FILENAME_LEN] = "out.csv\0";

    if(DEBUG){printf("\nsort v1.0");}

    //abort app upon no pars or help screen
    if(!readCLI(argc, argv, strFileTarget, strFileSrc, MAX_FILENAME_LEN)){printf("no params.\nType COMMAND -h for help screen"); return(0);}

    char* strWord;
    struct item* wRoot = NULL;

    fSrc = fileopen(strFileSrc, READ);

    //if file is open
    if(fSrc != (FILE *) NULL){
        wRoot = startList();
        if(!wRoot){if(DEBUG){printf("\ninvalid list storage address");}return((int) NULL);}

        if(DEBUG>1){getchar();printf("=====================================");}
        while(!boolEOF){
            //read word from file
            strWord = wordBuild(fSrc);

            //store read word into linked list
            if(((int) strWord) != 0){
                if(!(itemPopulate(strWord, wRoot))){
                    if(DEBUG){printf("\nword store aborted");}
                    break;
                }
            }
        }
        if(DEBUG){printf("\ndone reading words");}
        if(DEBUG>1){getchar();printf("=====================================");}

        fileclose(fSrc);

        //sort list
        sortBubble(wRoot, ASCENDING);
        //search for double entries
        filterClones(wRoot);

        //write list to destination file if accessable
        fDest = fileopen(strFileTarget, WRITE);
        if(fDest != (FILE *) NULL){
            //write list to file
            listWrite(wRoot, fDest);
            fileclose(fDest);
        }else{
            printf("\nunable to open file for writing."); getchar();
        }

        //start user interface
        uiRun(wRoot);

        //terminate list
        killList(wRoot);

        if(DEBUG){printf("\nend");}
        return(1);
    }else{
        printf("\ninvalid source file address");
        return(0);
    }
}
